<?php
/**
 *
 *----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by PhpStorm.
 * User: huangxh
 * Date: 2018/6/15
 * Time: 下午 3:11
 */

use models\DAO\GroupModel;
use Yaf\Registry;

class GroupController extends BaseController
{
    public function indexAction()
    {
        $this->display('index');
    }

    /**
     * 列表数据
     * @throws ReflectionException
     * @throws \Yaf\Exception
     */
    public function dataAction(){
        $groupModel = GroupModel::getInstance();
        $condition = [];
        if (!empty(Registry::get('parameters')['searchText'])) $condition = ['title' =>Registry::get("parameters")['searchText']];
        $sort = !empty(Registry::get('parameters')['sort'])? Registry::get("parameters")['sort']:'id';
        list($users,$mate) = $groupModel->getList(
            $condition,Registry::get("parameters")['pageNumber'],Registry::get("parameters")['pageSize'],
            [$sort=>Registry::get("parameters")['sortOrder']]
        );
        $this->returnJson(['rows'=>$users,'total'=>$mate],false);
    }

    /**
     * 删除规则
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function ruleDelAction(){
        $groupModel = GroupModel::getInstance();
        $result = $groupModel->deleteData(Registry::get("parameters")['id']);
        if ($result instanceof Exception) throw $result;
        $this->returnJson($result);
    }

    /**
     * 添加规则
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function groupAddAction()
    {
        $groupModel = GroupModel::getInstance();
        $data = [
            'title' => Registry::get("parameters")['title'],
            'status'  =>Registry::get("parameters")['status']
        ];
        $result =$groupModel->addData($data);
        if ($result instanceof Exception) throw $result;
        $this->Json([001,['message'=>$result]]);
    }

    /**
     * 修改规则
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function groupEditAction()
    {
        $groupModel = GroupModel::getInstance();
        $condition = [
            'id' => Registry::get("parameters")['id'],
            'title' => Registry::get("parameters")['title'],
            'status'  =>Registry::get("parameters")['status']
        ];
        $result = $groupModel->updateData($condition);
        if ($result instanceof Exception) throw $result;
        $this->Json([001,['message'=>$result]]);
    }

    /**
     * 修改权限
     * @throws Exception
     * @throws \Yaf\Exception
     */
    public function groupDataAction()
    {
        $groupModel = GroupModel::getInstance();
        $relus = empty(Registry::get("parameters")['rules'])?[1,11,19]:Registry::get("parameters")['rules'];
        $condition = [
            'id' => Registry::get("parameters")['id'],
            'rules' => implode(',',$relus)
        ];
        $result = $groupModel->updateData($condition);
        $this->Json([001,['message'=>$result]]);
    }
}